/******************************************************************************************************************
* File:ECSConsole.java
* Course: 17655
* Project: Assignment 3
* Copyright: Copyright (c) 2009 Carnegie Mellon University
* Versions:
*	1.0 February 2009 - Initial rewrite of original assignment 3 (ajl).
*
* Description: This class is the console for the museum environmental control system. This process consists of two
* threads. The ECSMonitor object is a thread that is started that is responsible for the monitoring and control of
* the museum environmental systems. The main thread provides a text interface for the user to change the temperature
* and humidity ranges, as well as shut down the system.
*
* Parameters: None
*
* Internal Methods: None
*
******************************************************************************************************************/
import TermioPackage.*;
import EventPackage.*;

public class SecurityConsole
{
	public static void main(String args[])
	{
    	Termio UserInput = new Termio();	// Termio IO Object
		boolean Done = false;				// Main loop flag
		String Option = null;				// Menu choice from user
		Event Evt = null;					// Event object
		boolean Error = false;				// Error flag
		ECSSMonitor Monitor = null;			// The environmental control system monitor
		float TempRangeHigh = (float)100.0;	// These parameters signify the temperature and humidity ranges in terms
		float TempRangeLow = (float)0.0;	// of high value and low values. The ECSmonitor will attempt to maintain
		float HumiRangeHigh = (float)100.0;	// this temperature and humidity. Temperatures are in degrees Fahrenheit
		float HumiRangeLow = (float)0.0;	// and humidity is in relative humidity percentage.
		String EvtMgrIP;				// Event Manager IP address
		EventManagerInterface em = null;// Interface object to the event manager

		/////////////////////////////////////////////////////////////////////////////////
		// Get the IP address of the event manager
		/////////////////////////////////////////////////////////////////////////////////
		
		if ( args.length == 0 )
 		{
			Monitor = new ECSSMonitor();
			//[KKT] Monitor thread Invoke
			// event manager is on the local system

			System.out.println("\n\nAttempting to register on the local machine..." );

			try
			{
				// Here we create an event manager interface object. This assumes
				// that the event manager is on the local machine

				em = new EventManagerInterface();
			}

			catch (Exception e)
			{
				System.out.println("Error instantiating event manager interface: " + e);

			} // catch

		} else {

			// event manager is not on the local system
			Monitor = new ECSSMonitor( args[0] );
 			//[KKT] Monitor thread Invoke

			EvtMgrIP = args[0];

			System.out.println("\n\nAttempting to register on the machine:: " + EvtMgrIP );

			try
			{
				// Here we create an event manager interface object. This assumes
				// that the event manager is NOT on the local machine

				em = new EventManagerInterface( EvtMgrIP );
			}

			catch (Exception e)
			{
				System.out.println("Error instantiating event manager interface: " + e);

			} // catch

		} // if


		// Here we check to see if registration worked. If ef is null then the
		// event manager interface was not properly created.

		if (Monitor.IsRegistered() )
		{
			Monitor.start(); // Here we start the monitoring and control thread

			while (!Done)
			{
				// Here, the main thread continues and provides the main menu

				System.out.println( "\n\n\n\n" );
				System.out.println( "Security Monitoring System (SMS) Command Console: \n" );

				if (args.length != 0)
					System.out.println( "Using event manger at: " + args[0] + "\n" );
				else
					System.out.println( "Using local event manger \n" );

				System.out.println( "1: [Windows Break]Set" );
				System.out.println( "2: [Windows Break]Confirm" );
				System.out.println( "3: [Door Break]Set" );
				System.out.println( "4: [Door Break]Confirm" );
				System.out.println( "5: [Motion Detection]Set" );
				System.out.println( "6: [Motion Detection]Confirm" );
				System.out.println( "X: Stop System\n" );
				System.out.print( "\n>>>> " );
				Option = UserInput.KeyboardReadString();

				//////////// option 1 ////////////

				if ( Option.equals( "1" ) )
				{
					// Here we get the temperature ranges

					Error = true;

					while (Error)
					{
						System.out.print( "\n\rSetting Windows Break Alarm..." );
						//Option = UserInput.KeyboardReadString();
						
						if (UserInput.IsNumber(Option))
						{
							Error = false;
							AlarmMessage(em, "W");
						} else {
							System.out.println( "Not a number, please try again..." );
						} // if
					} // while
				} // if

				//////////// option 2 ////////////
				if ( Option.equals( "2" ) )
				{
					// Here we get the temperature ranges

					Error = true;

					while (Error)
					{
						System.out.print( "\n\rConfiming Windows Break Alarm..." );
						//Option = UserInput.KeyboardReadString();
						
						if (UserInput.IsNumber(Option))
						{
							Error = false;
							AckMessage(em, "W");
						} else {
							System.out.println( "Not a number, please try again..." );
						} // if
					} // while
				} // if
				
				//////////// option 3 ////////////

				if ( Option.equals( "3" ) )
				{
					// Here we get the temperature ranges

					Error = true;

					while (Error)
					{
						System.out.print( "\n\rSetting Door Break Alarm..." );
						//Option = UserInput.KeyboardReadString();
						
						if (UserInput.IsNumber(Option))
						{
							Error = false;
							AlarmMessage(em, "D");
						} else {
							System.out.println( "Not a number, please try again..." );
						} // if
					} // while
				} // if

				//////////// option 4 ////////////
				if ( Option.equals( "4" ) )
				{
					// Here we get the temperature ranges

					Error = true;

					while (Error)
					{
						System.out.print( "\n\rConfiming Door Break Alarm..." );
						//Option = UserInput.KeyboardReadString();
						
						if (UserInput.IsNumber(Option))
						{
							Error = false;
							AckMessage(em, "D");
						} else {
							System.out.println( "Not a number, please try again..." );
						} // if
					} // while
				} // if		
				
				//////////// option 5 ////////////

				if ( Option.equals( "5" ) )
				{
					// Here we get the temperature ranges

					Error = true;

					while (Error)
					{
						System.out.print( "\n\rSetting Motion Detection Alarm..." );
						//Option = UserInput.KeyboardReadString();
						
						if (UserInput.IsNumber(Option))
						{
							Error = false;
							AlarmMessage(em, "M");
						} else {
							System.out.println( "Not a number, please try again..." );
						} // if
					} // while
				} // if

				//////////// option 6 ////////////
				if ( Option.equals( "6" ) )
				{
					// Here we get the temperature ranges

					Error = true;

					while (Error)
					{
						System.out.print( "\n\rConfiming Motion Detection Alarm..." );
						//Option = UserInput.KeyboardReadString();
						
						if (UserInput.IsNumber(Option))
						{
							Error = false;
							AckMessage(em, "M");
						} else {
							System.out.println( "Not a number, please try again..." );
						} // if
					} // while
				} // if	
				

				//////////// option X ////////////

				if ( Option.equalsIgnoreCase( "X" ) )
				{
					// Here the user is done, so we set the Done flag and halt
					// the environmental control system. The monitor provides a method
					// to do this. Its important to have processes release their queues
					// with the event manager. If these queues are not released these
					// become dead queues and they collect events and will eventually
					// cause problems for the event manager.

					Monitor.Halt();
					Done = true;
					System.out.println( "\nConsole Stopped... Exit monitor mindow to return to command prompt." );
					Monitor.Halt();

				} // if

			} // while

		} else {

			System.out.println("\n\nUnable start the monitor.\n\n" );

		} // if
		
		

  	} // main
	
	static private void AlarmMessage(EventManagerInterface ei, String m )
	{
		// Here we create the event.

		Event evt = new Event( (int) 6, m );

		// Here we send the event to the event manager.

		try
		{
			ei.SendEvent( evt );

		} // try

		catch (Exception e)
		{
			System.out.println("Error Confirming Message:: " + e);

		} // catch

	} // AlarmMessage
	
	static private void AckMessage(EventManagerInterface ei, String m )
	{
		// Here we create the event.

		Event evt = new Event( (int) -6, m );

		// Here we send the event to the event manager.

		try
		{
			ei.SendEvent( evt );

		} // try

		catch (Exception e)
		{
			System.out.println("Error Confirming Message:: " + e);

		} // catch

	} // AckMessage
	

} // ECSConsole
